package io;

import java.io.*;

/**
 * created by flyketch
 * 19-9-10 上午10:07
 */
public class FileOutputStreamTest {

    public static void main(String[] args) throws IOException {
//        test04();
//        test05();
//        test06();
//        test07();
//        test08();
        test09();
    }

    public static void test04() throws IOException {
        FileOutputStream fileOutputStream = new java.io.FileOutputStream(new File("").getAbsolutePath() + "/test.txt");

        byte[] bytes = {12, 23, 67, 87, 45};

        fileOutputStream.write(bytes);
        fileOutputStream.close();
    }


    public static void test05() throws IOException {
        FileInputStream fileInputStream = new FileInputStream(new File("").getAbsolutePath() + "/test.txt");

        int c;
        while ((c = fileInputStream.read()) != -1) {
            System.out.println(c);
        }
    }

    public static void test06() throws IOException {
        FileWriter fileWriter = new FileWriter(new File("").getAbsolutePath()+"/test.txt");
        fileWriter.write("Hello，world！\n欢迎来到 java 世界\n");
        fileWriter.write("不会覆盖文件原本的内容\n");
//        fileWriter.write(null); 不能直接写入 null
        fileWriter.append("并不是追加一行内容，不要被方法名迷惑\n");
        fileWriter.append(null);
        fileWriter.flush();
        System.out.println("文件的默认编码为" + fileWriter.getEncoding());
        fileWriter.close();
    }

    public static void test07() throws IOException {
        FileWriter fileWriter = new FileWriter(new File("").getAbsolutePath()+"/test.txt", false); // 关闭追加模式，变为覆盖模式
        fileWriter.write("Hello，world！欢迎来到 java 世界\n");
        fileWriter.write("我来覆盖文件原本的内容");
        fileWriter.append("我是下一行");
        fileWriter.flush();
        System.out.println("文件的默认编码为" + fileWriter.getEncoding());
        fileWriter.close();
    }

    public static void test08() throws IOException {
        FileReader fileReader = new FileReader(new File("").getAbsolutePath() + "/test.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        String str;
        while ( (str = bufferedReader.readLine()) != null ) {
            System.out.println(str);
        }

        bufferedReader.close();
        fileReader.close();
    }

    public static void test09() throws IOException {
        FileReader fileReader = new FileReader(new File("").getAbsolutePath() + "/test.txt");

        int c;
        while ((c = fileReader.read()) != -1) {
            System.out.println((char)c);
        }
    }

}
